import java.util.ArrayList;
import java.util.List;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
public class project {
    public int minFlips(String target) {
        int index=0;
        int ret=0;
        boolean flag=false;
        while (index<target.length()){
            if(target.charAt(index)=='0'&&flag){
                ret++;
                flag=false;
            }
            if(target.charAt(index)=='1'&&!flag){
                ret++;
                flag=true;
            }
            index++;
        }
        return ret;
    }
















    public TreeNode recoverFromPreorder(String traversal) {
        String[] str=traversal.split("-");
        List<TreeNode> list=new ArrayList<>();
        list.add(new TreeNode(Integer.valueOf(str[0])));
        int l=1;
        for(int i=1;i<str.length;i++){
            if(!str[i].isEmpty()){
                TreeNode node=new TreeNode(Integer.valueOf(str[i]));
                list.add(l,node);
                TreeNode prev=list.get(l-1);
                if(prev.left==null){
                    prev.left=node;
                }else{
                    prev.right=node;
                }
                l=1;
            }else{
                l++;
            }
        }
        return list.get(0);
    }











    public int minBitFlips(int start, int goal) {
        int tmp=start^goal;
        int ret=0;
        while (tmp>0){
            ret++;
            tmp&=tmp-1;
        }
        return ret;
    }
















    public int triangularSum(int[] nums) {
        int len=nums.length;
        int[][] dp=new int[len][len];
        dp[0]=nums;
        for(int i=1;i<len;i++){
            for(int j=0;j<len-i;j++){
                dp[i][j]=(dp[i-1][j]+dp[i-1][j+1])%10;
            }
        }
        return dp[len-1][0];
    }
















    public char nextGreatestLetter(char[] letters, char target) {
        for(int i=0;i<letters.length;i++){
            if(letters[i]>target){
                return letters[i];
            }
            if(i==letters.length-1){
                return letters[0];
            }
        }
        return ' ';
    }
}
